#!/usr/bin/env python
# coding: utf-8

# ## 上周小结：
# > * 1、初始高德API（Web服务）     
# > * 2、
# ## 本节知识点：
# > * 1 、地理编码/逆地理编码    
# > * 2、路径规划

# ### 2.1准备工作

# In[91]:


import requests,json
import pandas as pd
wy_key = "678a888f10d1b680e496b1b14cac9b38"


# ### 2.2地理编码/逆地理编码
# > * A.地理编码：将详细的结构化地址转换为高德经纬度坐标。且支持对地标性名胜景区、建筑物名称解析为高德经纬度坐标。     
# 结构化地址举例：北京市朝阳区阜通东大街6号转换后经纬度：116.480881,39.989410     
# 地标性建筑举例：天安门转换后经纬度：116.397499,39.908722        
# 逆地理编码：将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息。     
# 例如：116.480881,39.989410 转换地址描述后：北京市朝阳区阜通东大街6号        
# > * B.url:https://restapi.amap.com/v3/geocode/geo?parameters    
# > * C.关于request参数：    
# >> * 必填：key，address
# > * D.requests.get()

# In[92]:


# 地理编码 A-1
address = "广东省广州市中山大学南方学院"
wy_key = "678a888f10d1b680e496b1b14cac9b38"
url_geo = "https://restapi.amap.com/v3/geocode/geo?parameters"
params_geo = {
    "key":wy_key,
    "address":address
}
response = requests.get(url=url_geo,params=params_geo)


# In[93]:


response


# In[94]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# #### 2.2.2逆地理编码 A-2

# > * A.逆地理编码：将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息。
# > * B.url:https://restapi.amap.com/v3/geocode/regeo?parameters   
# > * C.关于request参数：    
# >> * 必填：key，location(经纬度坐标)
# >> * 选填：extensions(返回详细周边信息)
# > * D.requests.get()

# In[49]:


wy_key = "678a888f10d1b680e496b1b14cac9b38"
中大南方_location = "广东省广州市中山大学南方学院"
def regeo(wy_key,location,poitype=None,radius=None,extensions=None):
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":wy_key,
        "location":中大南方_location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
    }
    r = requests.get(url,params)
    return r.json


# In[50]:


regeo(wy_key,中大南方_location,extensions="all",radius=10000)


# In[51]:


url_regeo = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params_regeo = {
    "key":wy_key,
    "location":中大南方_location,
    "extensions":"all"
}
r = requests.get(url=url_regeo,params=params_regeo)


# In[52]:


r


# In[53]:


# 请取出所有的道路信息?用传统的编程思维？


# In[54]:


list_ = []
for i in r.json()['regeocode']['roads']:
    list_.append(i['name'])
list_


# In[55]:


r.json()


# In[58]:


pd.json_normalize(r.json())


# ### 2.3路径规划     
# > * 路径规划API是一套以HTTP形式提供的步行、公交、驾车查询及行驶距离计算接口，返回JSON 或 XML格式的查询数据，用于实现路径规划功能的开发。 
# > * 1、步行    
# > * 2、公交车     

# #### 步行    
# 
# > * 1.url:https://restapi.amap.com/v3/direction/walking?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,origin（出发点:经纬度），destination（目的地：经纬度）

# In[103]:


# 函数？！地理编码
# 位置参数，关键字参数（默认参数）
wy_key = "678a888f10d1b680e496b1b14cac9b38"
def geocode(key,address,city=None)->str:
    """获取高德API的地理编码信息
    key是高德web服务中API注册的密钥
    address是结构化地址，参考：https://lbs.amap.com/api/webservice/guide/api/georegeo"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
    "key":wy_key,
    "address":address,
    "city":city
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    # 返回值
    return results


# In[104]:


address = "广东省广州市中山大学南方学院"
address2 = "广东省广州市从化区龙岗镇"
中大南方_location = geocode(address=address,key=key,city="广州")
中大南方_location


# In[29]:


import time


# In[16]:


help(time)


# In[17]:


龙岗_location = geocode(wy_key,address2)
龙岗_location


# In[18]:


# 假如有一个刚会python的人，告诉他有一个函数可以用，你只需要提供高德的key和结构化地址
geocode("678a888f10d1b680e496b1b14cac9b38","北京市北京大学")


# In[19]:


# 获取地理编码（即经纬度）
address = "广东省广州市中山大学南方学院"
address2 = "广东省广州市从化区龙岗镇"
wy_key = "678a888f10d1b680e496b1b14cac9b38"
url_geo = "https://restapi.amap.com/v3/geocode/geo?parameters"
params_geo = {
    "key":wy_key,
    "address":address
}
response = requests.get(url=url_geo,params=params_geo)


# In[20]:


url_regeo = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params_regeo = {
    "key":wy_key,
    "location":address2,
}
r = requests.get(url=url_regeo,params=params_regeo)


# In[24]:


url_walking = "https://restapi.amap.com/v3/direction/walking?parameters"
params_walking = {
    "key":wy_key,
    "origin":中大南方_location,
    "destination":龙岗_location
}
r = requests.get(url_walking,params_walking)
r


# In[25]:


r.json()


# In[26]:


pd.json_normalize(r.json()['route']['paths'][0]['steps'])


# #### 公交    
# 
# > * 1.url:https://restapi.amap.com/v3/direction/transit/integrated?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,origin（出发点:经纬度），destination（目的地：经纬度），city

# In[38]:


origin = "113.350961,23.131577"
destination = "113.336127,23.113409"
city =  "广州"


# In[40]:


暨南大学_address = "广东省广州市暨南大学"
猎德大桥_address = "广东省广州市猎德大桥"
def bus(gao_key,origin,destination,city):
    gj_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":wy_key,
        "origin":origin,
        "destination":destination,
        "city":city,
    }
    r = requests.get(gj_url,params=params)
    result = r.json()
    return result


# In[41]:


a=bus(wy_key,origin,destination,city)
a


# In[42]:


pd.json_normalize(a["route"]["transits"])


# In[43]:


方案一 = pd.json_normalize(a["route"]["transits"][0])
方案一


# In[44]:


pd.json_normalize(a["route"]["transits"])["cost"]


# ### 2.4行政区规划       
# 行政区域查询是一类简单的HTTP接口，根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息。

# > * 1.url:https://restapi.amap.com/v3/config/district?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key

# In[46]:


url="https://restapi.amap.com/v3/config/district?parameters"
keywords="广东"
subdistrict="3"


# In[49]:


def xingzheng(wy_key,keywords,subdistrict):
    params = {
        "key":wy_key,
        "keywords":keywords,
        "output":"json",
        "subdistrict":subdistrict,
    }
    result = requests.get(url,params=params)
    return result


# In[50]:


b = xingzheng(wy_key,keywords,subdistrict)
b


# In[51]:


b.json()


# ### 2.5搜索POI     
# 搜索服务API是一类简单的HTTP接口，提供多种查询POI信息的能力，其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。

# #### 关键字搜索       
# 关键字搜索：通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行
# > * 1.url:https://restapi.amap.com/v3/place/text?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,keywords,types

# In[52]:


url="https://restapi.amap.com/v3/config/district?parameters"
keywords = "大学"
city = "广州"
citylimit = "true"


# In[53]:


def poi(keywords,city,citylimit=None):
    params={
    "key":wy_key,
    "keywords":keywords,
    "city":city,
    "citylimit":citylimit,
}
    result = requests.get(url,params=params)
    return result


# In[54]:


poi = poi(keywords,city,citylimit=None)
poi


# In[55]:


poi.json()


# #### 周边搜索    
# 周边搜索：在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索    
# > * 1.url:https://restapi.amap.com/v3/place/around?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,location

# In[56]:


url = "https://restapi.amap.com/v3/place/around?parameters "
location = "113.679287,23.632575"
city = "广州"


# In[57]:


def zhoubian(wy_key,location,city):
    params = {
        "key":wy_key,
        "location":location,
        "city":city,
    }
    r = requests.get(url,params=params)
    return r


# In[58]:


zb = zhoubian(wy_key,location,city)
zb.json()


# #### 多边形搜索    
# 多边形搜索：在多边形区域内进行搜索    
# > * 1.url:https://restapi.amap.com/v3/place/polygon?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,polygon

# In[59]:


url = "https://restapi.amap.com/v3/place/polygon?parameters "
polygon = "116.460988,40.006919|116.48231,40.007381;116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919"
keywords = "711"


# In[60]:


def duobianxing(wy_key,polygon,keywords):
    params = {
        "key":wy_key,
        "polygon":polygon,
        "keywords":keywords,
    }
    t = requests.get(url,params=params)
    return t


# In[61]:


db = duobianxing(wy_key,polygon,keywords)
db.json()


# #### ID查询   
# ID查询：通过POI ID，查询某个POI详情，建议可同输入提示API配合使用    
# > * 1.url:https://restapi.amap.com/v3/place/detail?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,id

# In[62]:


url = "https://restapi.amap.com/v3/place/detail?parameters "
id = "B0FFFAB6J2"


# In[63]:


def idchaxun(wy_key,id):
    params = {
        "key":wy_key,
        "id":id,
    }
    e = requests.get(url,params=params)
    return e


# In[64]:


idchaxun = idchaxun(wy_key,id)
idchaxun.json()


# ### 2.6IP定位     
# IP定位是一个简单的HTTP接口，根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。

# > * 1.url:https://restapi.amap.com/v3/ip?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key

# In[65]:


url = "https://restapi.amap.com/v3/ip?parameters"
ip = "119.29.115.93"


# In[66]:


def ipdingwei(wy_key,ip):
    params = {
        "key":wy_key,
        "ip":ip,
    }
    p = requests.get(url,params=params)
    return p


# In[67]:


ipdingwei = ipdingwei(wy_key,ip)
ipdingwei.json()


# ### 2.7静态地图    
# 静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。

# > * 1.url:https://restapi.amap.com/v3/staticmap?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,location,zoom

# In[69]:


url = "https://restapi.amap.com/v3/staticmap?parameters"
location = "113.679287,23.632575"
zoom = "1,17"
scale = "2"


# In[70]:


params={
    "key":wy_key,
    "location":location,
    "zoom":zoom,
    "scale":scale,
}
r = requests.get(url,params=params)


# In[71]:


r


# In[72]:


r.json()


# In[76]:


def jingtai(gao_key,location,zoom,scale):
    params ={
        "key":wy_key,
        "location":location,
        "zoom":zoom,
        "scale":scale,
    }
    r = requests.get(url,params=params)
    result = r.json()
    return result


# In[77]:


a = jingtai(wy_key,location,zoom,scale)
a


# ### 2.8坐标转换       
# 坐标转换是一类简单的HTTP接口，能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标。
# > * 1.url:https://restapi.amap.com/v3/assistant/coordinate/convert?parameters    
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,locations

# In[79]:


url="https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
locations="116.481499,39.990475|116.481499,39.990375"
coordsys="gps"


# In[80]:


def zuobiao(wy_key,locations,coordsys=None):
    params={
        "key":wy_key,
        "locations":locations,
        "coordsys":coordsys,
    }
    r=requests.get(url,params=params)
    return r


# In[81]:


e = zuobiao(wy_key,locations,coordsys=None)
e


# In[82]:


e.json()


# ### 2.9天气查询   
# 天气查询是一个简单的HTTP接口，根据用户输入的adcode，查询目标区域当前/未来的天气情况。
# > * 1.url:https://restapi.amap.com/v3/weather/weatherInfo?parameters   
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,city

# In[84]:


url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
city = "440100"
extensions="all"


# In[87]:


def tianqi(wy_key,city,extensions):
    params={
    "key":wy_key,
    "city":city,
    "extensions":extensions
    }
    r = requests.get(url,params=params)
    return r


# In[88]:


a = tianqi(wy_key,city,extensions)
a


# In[89]:


a.json()


# ### 2.10输入提示       
# 输入提示是一类简单的HTTP接口，提供根据用户输入的关键词查询返回建议列表
# > * 1.url:https://restapi.amap.com/v3/assistant/inputtips?parameters   
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,keywords

# In[94]:


keywords="KFC"
location="113.2759952545166,23.117055306224895"
city="广州"
citylimit="true"
output="json"
wy_key="678a888f10d1b680e496b1b14cac9b38"


# In[95]:


def tishi(wy_key,keywords,location,city):
    url="https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params={
        "keywords":keywords,
        "key":wy_key,
        "location":location,
        "city":city,
        "output":output,
    }
    t=requests.get(url,params=params)
    return t


# In[96]:


a = tishi(wy_key,keywords,location,city)
a.json()


# ### 2.11交通态势       
# 交通态势是一类简单的HTTP接口，提供根据用户输入的内容能够返回希望查询的交通态势情况。
# 
# 目前支持城市：北京，上海，广州，深圳，宁波，武汉，重庆，成都，沈阳，南京，杭州，长春，常州，大连，东莞，福州，青岛，石家庄，天津，太原，西安，无锡，厦门，珠海，长沙，苏州，金华，佛山，济南，泉州，嘉兴，西宁，惠州，温州，中山，合肥，乌鲁木齐，台州，绍兴，昆明。
# > * 1.url:https://restapi.amap.com/v3/traffic/status/rectangle?parameters   
# > * 2.requests.get()    
# > * 3.参数：   
# 
# >> * 必填：key,rectangle

# In[97]:


url="https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
extensions="all"


# In[102]:


def jiaotong(wy_key,level,extensions):
    params={
        "key":wy_key,
        "extensions":extensions,
        "level":level
    }
    r = requests.get(url,params=params)
    return r


# In[103]:


jiaotong = jiaotong(wy_key,level,extensions)
jiaotong.json()


# In[ ]:




